// pages/category/index.js
import { request } from "../../request/index"
Page({

  data: {
    leftlist: [],
    rightlist: [],
    currentindex: 0,
    scrolltop: 0
  },
  Cate: [],

  onLoad: function (options) {
    const cates = wx.getStorageSync("cate");
    this.Cate = cates.data
    if (!cates) {
      this.getcatelist()
    } else {
      const leftlist = cates.data.map(v => v.cat_name)
      const rightlist = cates.data[0].children
      this.setData({
        leftlist,
        rightlist
      })
    }


  },
  async getcatelist() {
    const Cate = await request({ url: "https://api-hmugo-web.itheima.net/api/public/v1/categories" })
    this.Cate = Cate.data.message
    wx.setStorageSync("cate", { time: Date.now(), data: this.Cate });

    const leftlist = this.Cate.map(v => v.cat_name)
    const rightlist = this.Cate[0].children
    this.setData({
      leftlist,
      rightlist
    })

  },
  handletap(e) {



    const { index } = e.currentTarget.dataset
    const rightlist = this.Cate[index].children
    this.setData({
      rightlist,
      currentindex: index,
      scrolltop: 0

    })

  }

})